package com.lyc.controller.user;

import com.lyc.DTO.InternshipJobDTO;
import com.lyc.pojo.InternshipJobs;
import com.lyc.pojo.Result;
import com.lyc.service.InternshipJobsService;
import com.lyc.service.impl.InternshipJobsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/Internships")
public class InternshipJobsController {

    // 创建日志记录器
    private static final Logger logger = LoggerFactory.getLogger(InternshipJobsController.class);
    
    @Autowired
    private InternshipJobsService jobService;
    
    /**
     * 发布实习/兼职信息
     * @param internshipJobDTO 实习/兼职信息
     * @param coverFile 封面图片
     * @return 结果
     */
    @PostMapping("/add")
    public Result addInternshipJob(InternshipJobDTO internshipJobDTO,
                                   @RequestParam(required = false) MultipartFile coverFile) {
        logger.info("接收到实习/兼职信息请求: {}", internshipJobDTO);
        logger.info("deadline字段值: {}", internshipJobDTO.getDeadline());
        logger.info("是否包含封面图片: {}", coverFile != null);
        // 打印jobTags参数
        logger.info("jobTags参数值: {}", internshipJobDTO.getJobTags());
        
        try {
            jobService.addInternshipJob(internshipJobDTO, coverFile);
            return Result.success("发布成功");
        } catch (IllegalArgumentException e) {
            // 处理参数验证错误
            logger.error("参数验证失败: ", e);
            return Result.error("参数验证失败: " + e.getMessage());
        } catch (IOException e) {
            // 处理文件上传错误
            logger.error("文件上传失败: ", e);
            return Result.error("文件上传失败: " + e.getMessage());
        } catch (Exception e) {
            // 处理其他系统错误
            logger.error("系统错误: ", e);
            return Result.error("系统错误: " + e.getMessage());
        }
    }
    
    /**
     * 获取实习/兼职信息列表
     * @return 实习/兼职信息列表
     */
    @GetMapping("/list")
    public Result<List<InternshipJobs>> getAllInternshipJobs() {
        logger.info("获取实习/兼职信息列表");
        try {
            List<InternshipJobs> jobs = jobService.getAllInternshipJobs();
            return Result.success(jobs);
        } catch (Exception e) {
            logger.error("获取实习/兼职信息列表失败: ", e);
            return Result.error("获取实习/兼职信息列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据行业获取实习/兼职信息列表
     * @param industry 行业
     * @return 实习/兼职信息列表
     */
    @GetMapping("/industry")
    public Result<List<InternshipJobs>> getIndustryList(@RequestParam String industry) {
        logger.info("根据行业获取实习/兼职信息列表: industry={}", industry);
        try {
            List<InternshipJobs> industryList = jobService.getIndustryList(industry);
            return Result.success(industryList);
        }
        catch (Exception e) {
            logger.error("获取行业领域列表失败: ", e);
            return Result.error("获取行业领域列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取实习/兼职信息详情
     * @param id 实习/兼职信息ID
     * @return 实习/兼职信息详情
     */
    @GetMapping("detail/{id}")
    public Result<InternshipJobs> getInternshipJobById(@PathVariable Integer id) {
        logger.info("根据ID获取实习/兼职信息详情: id={}", id);
        try {
            // 增加浏览次数
            jobService.incrementViewCount(id);
            
            InternshipJobs job = jobService.getInternshipJobById(id);
            if (job != null) {
                return Result.success(job);
            } else {
                return Result.error("未找到指定的实习/兼职信息");
            }
        } catch (Exception e) {
            logger.error("获取实习/兼职信息详情失败: ", e);
            return Result.error("获取实习/兼职信息详情失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据postId获取实习/兼职信息详情
     * @param postId 帖子ID
     * @return 实习/兼职信息详情
     */
    @GetMapping("detail/post/{postId}")
    public Result<InternshipJobs> getInternshipJobByPostId(@PathVariable Integer postId) {
        logger.info("根据postId获取实习/兼职信息详情: postId={}", postId);
        try {
            InternshipJobs job = ((InternshipJobsServiceImpl) jobService).getInternshipJobByPostId(postId);
            if (job != null) {
                return Result.success(job);
            } else {
                return Result.error("未找到指定的实习/兼职信息");
            }
        } catch (Exception e) {
            logger.error("获取实习/兼职信息详情失败: ", e);
            return Result.error("获取实习/兼职信息详情失败: " + e.getMessage());
        }
    }
}